package org.xxforest.game.core.scenes.scene2d.ui;

import org.xxforest.game.core.utils.XXPoint;

public class XXRect {

	private float x ;
	private float y ;
	private float width ;
	private float height ;
	
	private XXRect(float x, float y, float width, float height) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
	}
	
	public static XXRect create(float x , float y , float width , float height){
		return new XXRect(x,y,width,height) ;
	}

	public static XXRect create(XXPoint xxPoint , float width , float height){
		return new XXRect(xxPoint.x,xxPoint.y,width,height) ;
	}
	
	public static XXRect create(XXRect xxRect){
		return new XXRect(xxRect.x,xxRect.y,xxRect.width,xxRect.height) ;
	}
	
	public static XXRect create(){
		return new XXRect(0,0,0,0) ;
	}
	
	public float getX() {
		return x;
	}
	public void setX(float x) {
		this.x = x;
	}
	public float getY() {
		return y;
	}
	public void setY(float y) {
		this.y = y;
	}
	public float getWidth() {
		return width;
	}
	public void setWidth(float width) {
		this.width = width;
	}
	public float getHeight() {
		return height;
	}
	public void setHeight(float height) {
		this.height = height;
	}
	public XXPoint getPoint(){
		return XXPoint.create(x,y);
	}
	/**
	 * 是否重叠
	 * @return
	 */
	public boolean overlaps(XXRect xxRect){
		return !(x > xxRect.x + xxRect.width || x + width < xxRect.x || y > xxRect.y + xxRect.height || y + height < xxRect.y);
	}
	
	public void merge (XXRect rect) {
		float minX = Math.min(x, rect.x);
		float maxX = Math.max(x + width, rect.x + rect.width);
		x = minX;
		width = maxX - minX;

		float minY = Math.min(y, rect.y);
		float maxY = Math.max(y + height, rect.y + rect.height);
		y = minY;
		height = maxY - minY;
	}

	public XXRect offset(int offSetX, int offSetY) {
		x += offSetX ;
		y += offSetY ;
		return this;
	}
}
